Python可视化

您所在的位置:网站首页 泰勒图 python Python可视化

Python可视化

2024-03-29 07:19| 来源: 网络整理| 查看: 265

泰勒图绘制的核心思想是设计一个只有第一象限的极坐标,并将方差,相关系数进行捆绑,通过转化为极坐标系坐标进行绘制。为了实现泰勒图的绘制,设计了两个函数:

set_tayloraxes(fig, location=111) plot_taylor(axes, refsample, sample, args, *kwargs)

set_tayloraxes()函数用于建立一个泰勒图的坐标系,这个自定义函数一般情况下不建议修改,每一个参数都是经过多次调试得到的,很可能牵一发动全身。因此,将绘图部分的独立成为了plot_taylor函数(),这部分函数较为简单,目的就是将需要绘图的数据,转换为极坐标系坐标,通过plot函数将散点打在泰勒图上,这个函数模块较为简单,可以根据自己的输入数据情况进行调整。

下面直接给出两个函数的完整代码:

from matplotlib.projections import PolarAxes from mpl_toolkits.axisartist import floating_axes from mpl_toolkits.axisartist import grid_finder import numpy as np def set_tayloraxes(fig, location=111): trans = PolarAxes.PolarTransform() r1_locs = np.hstack((np.arange(1,10)/10.0,[0.95,0.99])) t1_locs = np.arccos(r1_locs) gl1 = grid_finder.FixedLocator(t1_locs) tf1 = grid_finder.DictFormatter(dict(zip(tlocs, map(str,rlocs)))) r2_locs = np.arange(0,2,0.25) r2_labels = ['0 ', '0.25 ', '0.50 ', '0.75 ', 'REF ', '1.25 ', '1.50 ', '1.75 '] gl2 = grid_finder.FixedLocator(r2_locs) tf2 = grid_finder.DictFormatter(dict(zip(r2_locs, map(str,r2_labels)))) ghelper = floating_axes.GridHelperCurveLinear(tr,extremes=(0,np.pi/2,0,1.75), grid_locator1=gl1,tick_formatter1=tf1, grid_locator2=gl2,tick_formatter2=tf2) ax = floating_axes.FloatingSubplot(fig, location, grid_helper=ghelper) fig.add_subplot(ax) ax.axis["top"].set_axis_direction("bottom") ax.axis["top"].toggle(ticklabels=True, label=True) ax.axis["top"].major_ticklabels.set_axis_direction("top") ax.axis["top"].label.set_axis_direction("top") ax.axis["top"].label.set_text("Correlation") ax.axis["left"].set_axis_direction("bottom") ax.axis["left"].label.set_text("Standard deviation") ax.axis["right"].set_axis_direction("top") ax.axis["right"].toggle(ticklabels=True) ax.axis["right"].major_ticklabels.set_axis_direction("left") ax.axis["bottom"].set_visible(False) ax.grid() polar_ax = ax.get_aux_axes(trans) t = np.linspace(0,np.pi/2) r = np.zeros_like(t) + 1 polar_ax.plot(t,r,'k--') polar_ax.text(np.pi/2+0.042,1.03, " 1.00", size=10.5,ha="right", va="top", bbox=dict(boxstyle="square",ec='w',fc='w')) return polar_ax def plot_taylor(axes, refsample, sample, *args, **kwargs): std = np.std(sample) corr = np.corrcoef(refsample, sample) theta = np.arccos(corr[0,1]) t,r = theta,std d = axes.plot(t,r, *args, **kwargs) return d

下面介绍下函数的具体用法:

setup_axes(fig, rect=111)

输入:

fig:需要绘图的figure

rect:图的位置,如111为1行1列第一个,122为1行2列第2个

输出:

polar_ax:泰勒坐标系

plot_taylor(axes, refsample, sample, *args, **kwargs)

输入:

axes : setup_axes返回的泰勒坐标系

refsample :参照样本

sample :评估样本

args, *kwargs :plt.plot()函数的相关参数,设置点的颜色,形状等等。

下面给出示例:

x = np.linspace(0,10*np.pi,100)data = np.sin(x) m1 = data + 0.4*np.random.randn(len(x)) m2 = 0.3*data + 0.6*np.random.randn(len(x)) m3 = np.sin(x-np.pi/10) fig = plt.figure(figsize=(10,4))ax1 = set_tayloraxes(fig, 121)ax2 = set_tayloraxes(fig, 122)d1 = plot_taylor(ax2,data,m1, 'bo')d2 = plot_taylor(ax2,data,m2, 'ro')d3 = plot_taylor(ax2,data,m3, 'go')


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3